package com.zx.platform.controller;

import com.zx.platform.context.DirectoryInfoContext;
import com.zx.platform.service.DirectoryService;
import com.zx.platform.service.domain.DirectoryModel;
import com.zx.platform.service.domain.ManageSessionDomain;
import com.zx.platform.utils.PmpUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统参数管理
 *
 * @author Qizhan Lu
 * @version 1.0
 */
@Controller
@RequestMapping("/manage/directory")
public class DirectoryController {
    /**
     * log
     */
    private static Logger log = Logger.getLogger(DirectoryController.class);
    @Autowired
    private DirectoryService directoryService;

    @RequestMapping("/list")
    public String list(DirectoryModel directoryModel, Model model) {
        // 初始化信息
        Integer pageNum = directoryModel.getPageNum();
        Integer pageSize = directoryModel.getPageSize();
        Integer pageCount = directoryModel.getPageCount();
        if (null == pageNum ) {
            pageNum = 1;
        }
        if (null == pageSize ) {
            pageSize = 10;
        }
        Map map = directoryService.getDirectory(pageSize, pageNum, directoryModel);
        List<DirectoryModel> directoryModelList = (List<DirectoryModel>) map.get("list");
        int recordCount = Integer.valueOf(map.get("count").toString());
        // 页面信息
        if (pageSize * pageNum > recordCount + pageSize - 1) {
            pageNum = 1;
        }
        model.addAttribute("list", directoryModelList);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("pageCount", PmpUtils.getPageCount(recordCount, pageSize));
        model.addAttribute("recordCount", recordCount);
        model.addAttribute("model", directoryModel);
        return "/manage/directlist";

    }


    /**
     * @return
     */
    @RequestMapping("/update")
    public
    @ResponseBody
    DirectoryModel update(DirectoryModel directoryModel) {
        try {
            directoryService.updateDirectory(directoryModel);
            directoryModel.setSuccess(true);
            directoryModel.setMsg("保存信息成功");
            log.debug("保存信息成功");
            DirectoryInfoContext.initialize();
        } catch (Exception e) {
            directoryModel.setSuccess(false);
            directoryModel.setMsg("保存信息失败");
            log.error("保存信息失败" + e.getMessage(), e);
        }
        return directoryModel;
    }

    /**
     * @return
     */
    @RequestMapping("/delete")
    public
    @ResponseBody
    DirectoryModel delete(DirectoryModel directoryModel) {
        try {
            directoryService.deleteDirectory(directoryModel);
            directoryModel.setSuccess(true);
            directoryModel.setMsg("删除信息成功");
            log.debug("删除信息成功");
        } catch (Exception e) {
            directoryModel.setSuccess(false);
            directoryModel.setMsg("删除信息失败");
            log.error("删除信息失败" + e.getMessage(), e);
        }
        return directoryModel;
    }

    /**
     * @return
     */
    @RequestMapping("/add")
    public
    @ResponseBody
    DirectoryModel add(DirectoryModel directoryModel) {
        try {
            directoryService.addDirectory(directoryModel);
            directoryModel.setSuccess(true);
            directoryModel.setMsg("新增信息成功");
            log.debug("新增信息成功");
            DirectoryInfoContext.initialize();
        } catch (Exception e) {
            directoryModel.setSuccess(false);
            directoryModel.setMsg("新增信息失败");
            log.error("新增信息失败" + e.getMessage(), e);
        }
        return directoryModel;
    }
    /**
     * @return
     */
    @RequestMapping("/getModelById")
    public
    @ResponseBody
    Map getModelById(HttpServletRequest request) {
        Map map = new HashMap();
        try {
            // 获得登陆时的用户
            ManageSessionDomain userinfo = (ManageSessionDomain) request.getSession().getAttribute("_LOGONUSER_");
            DirectoryModel directoryModel=new DirectoryModel(); 
            String dirIdStr = request.getParameter("dirId");
            if(StringUtils.isNotBlank(dirIdStr)){
            	Integer dirId = Integer.valueOf(dirIdStr);
            	directoryModel.setDirId(dirId);
            }
            directoryModel = directoryService.getModelById(directoryModel);
            if(directoryModel!=null){
            	map.put("model", directoryModel);
            }else{
            	map.put("model",null);
            }
            map.put("state", 1);
        } catch (Exception e) {
            map.put("state", 0);
            log.error("查询相关信息失败" + e.getMessage(), e);
        }
        return map;
    }

    /**
     * @return
     */
    @RequestMapping("/getDirListByKey")
    @ResponseBody
    public Map getDirListByKey(DirectoryModel model) {
        try {
        	List list = directoryService.getDirectoryListByKey(model.getDirKey());
        	model.setSuccess(true);
        	model.setMsg("新增信息成功");
            log.debug("新增信息成功");
            Map map = new HashMap();
            map.put("success", true);
            map.put("list", list);
            return map;
        } catch (Exception e) {
        	model.setSuccess(false);
        	model.setMsg("新增信息失败");
            log.error("新增信息失败" + e.getMessage(), e);
        }
        return null;
    }
}